package hust.ioic.oa.service.impl;

import java.util.List;

import javax.annotation.Resource;

import hust.ioic.oa.service.OperatorService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.codec.digest.DigestUtils;

import hust.ioic.oa.base.BaseDao;
import hust.ioic.oa.base.BaseDaoImpl;
import hust.ioic.oa.domain.Operator;

@Service
@Transactional
@SuppressWarnings("unchecked")
public class OperatorServiceImpl extends BaseDaoImpl<Operator> implements OperatorService {

	@Override
	public Operator findByUsernameAndPassowrd(String username, String password) {
		String md5Digest = DigestUtils.md5Hex(password);
		
		return (Operator) getSession()//
				.createQuery("From Operator o WHERE o.username=? AND o.password=? AND o.status=? ")//
				.setParameter(0, username)//
				.setParameter(1, md5Digest)//
				.setParameter(2, 1)
				.uniqueResult();
	}

	@Override
	public List<Operator> findAllByStatus() {
		
		return getSession()//
				.createQuery("From Operator o WHERE o.status=?  ")//
				.setParameter(0, 1)//
				.list();
	}



	@Override
	public List<String> getAllOperatorNames() {
		// TODO Auto-generated method stub
		return getSession()
				   .createQuery("SELECT  o.username FROM Operator o WHERE o.username IS NOT NULL ")
				   .list();
	}
}
